package com.xinqi.modules.course.clazz.controller.inner;

import com.xinqi.common.base.response.R;
import com.xinqi.common.base.user.Account;
import com.xinqi.common.core.enums.ClassRoleEnum;
import com.xinqi.common.satoken.utils.LoginHelper;
import com.xinqi.modules.course.clazz.dto.req.ClassStudentQueryDTO;
import com.xinqi.modules.course.clazz.dto.rsp.ClassStudentResultDTO;
import com.xinqi.modules.course.clazz.service.ClassStudentService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 班级学生表控制层
 *
 * @author: system
 * @date: 2023/01/17
 */
@RestController
@RequestMapping("/inner/class-student")
@RequiredArgsConstructor
public class ClassStudentInnerController {
    private final ClassStudentService classStudentService;

    @Operation(summary = "根据学生id删除他所有班级下的自己以及所有小组内的自己")
    @PostMapping("/{studentId}/delete")
    public R<Boolean> deleteByStudentId(@PathVariable("studentId") Long studentId) {
        Account account;
        try {
            account = LoginHelper.getXqLoginUser();
        } catch (Exception e) {
            account = LoginHelper.getXqLoginUser(studentId);
        }

        Boolean result = classStudentService.deleteByStudentId(account, studentId);
        return R.data(result);
    }

    @Operation(summary = "根据用户ID和班级ID获取学生信息")
    @GetMapping("/info/{studentId}/{classId}")
    public R<ClassStudentResultDTO> findByClassIdAndStudentId(@PathVariable("studentId") Long studentId, @PathVariable("classId") Long classId) {
        return R.data(classStudentService.findByClassIdAndStudentId(classId, studentId));
    }

    @Operation(summary = "获取学生List信息")
    @PostMapping("/list")
    public R<List<ClassStudentResultDTO>> findClassStudent(@RequestBody ClassStudentQueryDTO query) {
        return R.data(classStudentService.find(query));
    }

    @Operation(summary = "校验用户权限")
    @GetMapping("/checkRole/{classId}/{userId}")
    public R<ClassStudentResultDTO> checkRole(@PathVariable("classId") Long classId, @PathVariable("userId") Long userId, @RequestParam("classRoles") List<ClassRoleEnum> classRoles) {
        return R.data(classStudentService.checkRole(classId, userId, classRoles));
    }

    @Operation(summary = "获取班级学生老师列表不包括注销用户")
    @GetMapping("/findContainsTeacherListByClassId/{classId}")
    public R<List<ClassStudentResultDTO>> findContainsTeacherListByClassId(@PathVariable("classId") Long classId) {
        return R.data(classStudentService.findContainsTeacherListByClassId(classId));
    }

    @Operation(summary = "校验是班级里的学生")
    @GetMapping("/checkIsStudent/{classId}/{studentId}")
    public R<Boolean> checkIsStudent(@PathVariable("classId") Long classId, @PathVariable("studentId") Long studentId) {
        return R.data(classStudentService.checkIsStudent(classId, studentId));
    }

    @Operation(summary = "获取班级学生列表不包括注销学生")
    @GetMapping("/studentListByClassId/{classId}")
    public R<List<ClassStudentResultDTO>> studentListByClassId(@PathVariable("classId") Long classId) {
        return R.data(classStudentService.studentListByClassId(classId));
    }

    @Operation(summary = "获取班级成员列表不包括注销成员")
    @GetMapping("/allUserListByClassId/{classId}")
    public R<List<ClassStudentResultDTO>> allUserListByClassId(@PathVariable("classId") Long classId) {
        return R.data(classStudentService.allUserListByClassId(classId));
    }

    @GetMapping("/findByClassIdListAndRoleList/")
    R<List<ClassStudentResultDTO>> findByClassIdListAndRoleList(@RequestParam("classIdList") List<Long> classIdList, @RequestParam("roles") List<ClassRoleEnum> roles) {
        return R.data(classStudentService.findByClassIdListAndRoleList(classIdList, roles));
    }

    @GetMapping("/findByStudentId/{classId}/{userId}")
    R<ClassStudentResultDTO> findByStudentId(@PathVariable("classId") Long classId, @PathVariable("userId") Long userId) {
        return R.data(classStudentService.findByStudentId(classId, userId));
    }

    @GetMapping("/findByStudentIds/{classId}")
    R<List<ClassStudentResultDTO>> findByStudentIds(@PathVariable("classId") Long classId, @RequestParam("userIds") List<Long> userIds) {
        return R.data(classStudentService.findStudentByStudentIdAndClassId(classId, userIds));
    }

    @GetMapping("/find-all-roles-student/{classId}/{studentId}")
    public R<ClassStudentResultDTO> findAllRolesStudentByClassIdAndStudentId(@PathVariable("classId") Long classId, @PathVariable("studentId") Long studentId) {
        return R.data(classStudentService.findAllRolesStudentByClassIdAndStudentId(classId, studentId));
    }

    /**
     * 根据班级id和关系用户id查询主学生信息(只查询主学生信息)
     *
     * @param userId 主学生ID集合
     * @return 模拟学生id
     */
    @GetMapping("/find-virtual-class-student/{classId}/{userId}")
    public R<ClassStudentResultDTO> findVirtualClassStudentByClassIdAndUserId(@PathVariable("classId") Long classId, @PathVariable("userId") Long userId) {
        return R.data(classStudentService.findVirtualClassStudentByClassIdAndUserId(classId, userId));
    }

    /**
     * 根据班级id和主学生ID集合查询班级学生(只统计关系学生)
     *
     * @param userIds 主学生ID集合
     * @return 模拟学生id
     */
    @PostMapping("/find-class-relationship-student-list/{classId}")
    public R<List<ClassStudentResultDTO>> findClassRelationshipStudentList(@PathVariable("classId") Long classId, @RequestBody List<Long> userIds) {
        return R.data(classStudentService.findRelationshipStudentByClassIdAndUserIds(classId, userIds));
    }

    /**
     * 根据班级id查询班级总学生(只统计模拟主学生)
     *
     * @param classId 班级id
     * @return Long
     */
    @Operation(summary = "根据班级id查询班级总学生(只统计模拟主学生)")
    @GetMapping("/find-class-master-student-list")
    public R<List<ClassStudentResultDTO>> findClassMasterStudentByClassId(@RequestParam("classId") Long classId) {
        List<ClassStudentResultDTO> list = classStudentService.findClassMasterStudentByClassId(classId);
        return R.data(list);
    }

    /**
     * 根据班级id列表查询班级总学生(只统计模拟主学生)
     *
     * @param classIdList 班级id列表
     * @return Long
     */
    @Operation(summary = "根据班级id列表查询班级总学生(只统计模拟主学生)")
    @PostMapping("/find-class-master-student-list-by-classIds")
    public R<List<ClassStudentResultDTO>> findClassMasterStudentByClassIdList(@RequestBody List<Long> classIdList) {
        List<ClassStudentResultDTO> list = classStudentService.findClassMasterStudentByClassIdList(classIdList);
        return R.data(list);
    }

}
